package gw.com.android.ui.trade;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.gwtsz.gts2.hx.R;

import java.util.Calendar;

import gw.com.android.app.GTConfig;
import gw.com.android.ui.views.GuideView;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-04-24.
 */
class OrderOpenGuide {

    private Toast toast;

    void show(Activity context, View root, View firstAnchor, View secondAnchor) {
        if (context == null || firstAnchor == null || secondAnchor == null) {
            return;
        }
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        if (hour >= 20 || hour <= 6) {
            showNightGuide(context, root);
        } else {
            boolean orderGuideShowed = GTConfig.instance().getBooleanValue("order_dialog_guide", false);
            if (orderGuideShowed) {
                return;
            }
            int dimension = (int) context.getResources().getDimension(R.dimen.toolbar_top_space);
            int[] location = new int[2];
            firstAnchor.getLocationOnScreen(location);
            int offset = (DeviceUtil.instance().dip2px(36, context) - firstAnchor.getHeight()) / 2;
            //int firstY = location[1] - dimension - DeviceUtil.instance().dip2px(10, context);
            int firstY = location[1] - dimension - offset - DeviceUtil.instance().dip2px(3, context);
            if (firstY <= 0) {
                return;
            }
            secondAnchor.getLocationOnScreen(location);
            offset = (DeviceUtil.instance().dip2px(34, context) - secondAnchor.getHeight()) / 2;
            //int secondY = location[1] - dimension - DeviceUtil.instance().dip2px(12, context);
            int secondY = location[1] - dimension - offset - DeviceUtil.instance().dip2px(4, context);
            if (secondY <= 0) {
                return;
            }
            showDayGuide(context, firstY, secondY);
            GTConfig.instance().setBooleanValue("order_dialog_guide", true);
        }
    }

    void dismiss() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

    private void showNightGuide1(Activity context, View root) {
        ImageView iv = new ImageView(context);
        iv.setPadding(DeviceUtil.instance().dip2px(10, context), 0, DeviceUtil.instance().dip2px(10, context), 0);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(R.mipmap.a_pop_guide);

        PopupWindow pw = new PopupWindow(iv, DeviceUtil.instance().getScreenPixelsWidth(context), DeviceUtil.instance().dip2px(80, context));
        pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        pw.setFocusable(true);
        pw.setOutsideTouchable(true);
        pw.setAnimationStyle(R.style.popp_anim);
        //pw.showAsDropDown(context.getWindow().getDecorView(), 0, -200);
        pw.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM, 0, root.getHeight() + DeviceUtil.instance().dip2px(80, context));
    }

    private void showNightGuide2(Activity context, View root) {
        ImageView iv = new ImageView(context);
        iv.setPadding(DeviceUtil.instance().dip2px(10, context), 0, DeviceUtil.instance().dip2px(10, context), 0);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(R.mipmap.a_pop_guide);

        WindowManager mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
        mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.height = DeviceUtil.instance().dip2px(80, context);
        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.windowAnimations = android.R.style.Animation_Toast;
        mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        mParams.gravity = Gravity.BOTTOM;
        mParams.x = 0;
        mParams.y = root.getHeight() + DeviceUtil.instance().dip2px(10, context);
        mParams.horizontalMargin = DeviceUtil.instance().dip2px(10, context);

        mWM.addView(iv, mParams);
    }

    private void showNightGuide3(Activity context, View root) {
        ImageView iv = new ImageView(context);
        iv.setPadding(DeviceUtil.instance().dip2px(10, context), 0, DeviceUtil.instance().dip2px(10, context), 0);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(R.mipmap.a_pop_guide);

        Dialog dialog = new Dialog(context, R.style.dialog_popupwindow);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dialog.setContentView(iv, contentParam);
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = DeviceUtil.instance().getScreenPixelsWidth(context);
            lp.height = DeviceUtil.instance().dip2px(80, context);
            lp.gravity = Gravity.BOTTOM;
            lp.x = 0;
            lp.y = root.getHeight() + DeviceUtil.instance().dip2px(10, context);
            window.setAttributes(lp);
        }
        dialog.show();
    }

    private void showNightGuide(Activity context, View root) {
        ImageView iv = new ImageView(context);
        iv.setPadding(DeviceUtil.instance().dip2px(10, context), 0, DeviceUtil.instance().dip2px(10, context), 0);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setImageResource(R.mipmap.a_pop_guide);

        toast = new Toast(context);
        toast.setView(iv);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL, 0, root.getHeight() + DeviceUtil.instance().dip2px(10, context));

        toast.show();
    }

    private void showDayGuide(Context context, int firstY, int secondY) {
        View root = View.inflate(context, R.layout.view_order_open_guide, null);
        final GuideView firstGuide = (GuideView) root.findViewById(R.id.order_guide1);
        final View firstMask = root.findViewById(R.id.order_guide_mask1);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) firstMask.getLayoutParams();
        params.topMargin = firstY;
        firstMask.setLayoutParams(params);
        firstGuide.addUncoverView(firstMask);
        int osmosis = DeviceUtil.instance().dip2px(1F, context);
        firstGuide.setOsmosis(osmosis, osmosis, osmosis, osmosis);

        final GuideView secondGuide = (GuideView) root.findViewById(R.id.order_guide2);
        final View secondMask = root.findViewById(R.id.order_guide_mask2);
        params = (ViewGroup.MarginLayoutParams) secondMask.getLayoutParams();
        params.topMargin = secondY;
        secondMask.setLayoutParams(params);
        secondGuide.addUncoverView(secondMask);
        secondGuide.setOsmosis(osmosis, osmosis, osmosis, osmosis);

        final Dialog dialog = new Dialog(context, R.style.dialog_popupwindow);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        dialog.setContentView(root, contentParam);
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = DeviceUtil.instance().getScreenPixelsWidth(context);
            lp.height = DeviceUtil.instance().getScreenPixelsHeight(context);
            window.setAttributes(lp);
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        firstGuide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firstGuide.setVisibility(View.GONE);
                secondGuide.setVisibility(View.VISIBLE);
            }
        });
        secondGuide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}
